﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.InfoCardClaimCollection
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace Microsoft.InfoCards
{
  internal class InfoCardClaimCollection : Dictionary<string, InfoCardClaim>
  {
    private const int InvalidRow = 0;
    private Uri m_infoCardId;
    private int m_rowId;

    private InfoCardClaimCollection()
    {
    }

    public InfoCardClaimCollection(Uri infoCardId)
      : this()
    {
      if ((Uri) null == infoCardId)
        throw InfoCardTrace.ThrowHelperArgumentNull(nameof (infoCardId));
      this.m_infoCardId = infoCardId;
    }

    public void Add(InfoCardClaim claim)
    {
      if (claim == null)
        throw InfoCardTrace.ThrowHelperArgumentNull(nameof (claim));
      this[claim.Id] = claim;
    }

    public void Get(StoreConnection con)
    {
      if (con == null)
        throw InfoCardTrace.ThrowHelperArgumentNull(nameof (con));
      DataRow row = this.TryGetRow(con, QueryDetails.FullRow);
      if (row == null)
        return;
      this.Deserialize((Stream) new MemoryStream(row.GetDataField()));
      this.m_rowId = row.LocalId;
    }

    public void Serialize(Stream stream, bool selfIssued)
    {
      BinaryWriter writer = new BinaryWriter(stream, Encoding.Unicode);
      int count = this.Count;
      if (selfIssued && this.ContainsKey(InfoCardConstants.PPIDClaimsUri))
        --count;
      writer.Write(count);
      foreach (KeyValuePair<string, InfoCardClaim> keyValuePair in (Dictionary<string, InfoCardClaim>) this)
      {
        if (selfIssued)
        {
          if (InfoCardConstants.PPIDClaimsUri != keyValuePair.Key)
            keyValuePair.Value.Serialize(writer);
        }
        else
          keyValuePair.Value.Serialize(writer);
      }
    }

    public void AgentSerialize(Stream stream)
    {
      BinaryWriter writer = new BinaryWriter(stream, Encoding.Unicode);
      writer.Write(this.Count);
      foreach (KeyValuePair<string, InfoCardClaim> keyValuePair in (Dictionary<string, InfoCardClaim>) this)
        keyValuePair.Value.Serialize(writer);
    }

    public void Deserialize(Stream stream)
    {
      BinaryReader reader = (BinaryReader) new InfoCardBinaryReader(stream, Encoding.Unicode);
      int num = reader.ReadInt32();
      for (int index = 0; index < num; ++index)
      {
        InfoCardClaim infoCardClaim = new InfoCardClaim();
        infoCardClaim.DeSerialize(reader);
        this[infoCardClaim.Id] = infoCardClaim;
      }
    }

    public void Save(StoreConnection con, bool selfIssued)
    {
      if (con == null)
        throw InfoCardTrace.ThrowHelperArgumentNull(nameof (con));
      DataRow row = this.TryGetRow(con, QueryDetails.FullHeader);
      if (row == null)
      {
        row = new DataRow();
        row.ObjectType = 2;
        row.GlobalId = (GlobalId) Guid.NewGuid();
      }
      row.SetIndexValue("ix_parentid", (object) GlobalId.DeriveFrom(this.m_infoCardId.ToString()));
      MemoryStream memoryStream = new MemoryStream();
      this.Serialize((Stream) memoryStream, selfIssued);
      row.SetDataField(memoryStream.ToArray());
      con.Save(row);
      this.m_rowId = row.LocalId;
    }

    public void Encrypt(PinProtectionHelper pinHelper)
    {
      foreach (KeyValuePair<string, InfoCardClaim> keyValuePair in (Dictionary<string, InfoCardClaim>) this)
      {
        if (keyValuePair.Value.Value.Length > 0)
          keyValuePair.Value.Encrypt(pinHelper);
      }
    }

    public void Decrypt(PinProtectionHelper pinHelper)
    {
      foreach (KeyValuePair<string, InfoCardClaim> keyValuePair in (Dictionary<string, InfoCardClaim>) this)
      {
        if (keyValuePair.Value.Value.Length > 0)
        {
          keyValuePair.Value.Decrypt(pinHelper);
          if (!string.IsNullOrEmpty(keyValuePair.Value.Value))
          {
            if (InfoCardConstants.Gender == keyValuePair.Key && (!("0" == keyValuePair.Value.Value) && !("1" == keyValuePair.Value.Value) && !("2" == keyValuePair.Value.Value)))
              throw InfoCardTrace.ThrowHelperError((Exception) new InvalidCardException(SR.GetString("ServiceInvalidEncryptedClaimValues")));
            if (InfoCardConstants.DateOfBirth == keyValuePair.Key)
            {
              try
              {
                XmlConvert.ToDateTime(keyValuePair.Value.Value, XmlDateTimeSerializationMode.Utc);
              }
              catch (Exception ex)
              {
                if (!InfoCardTrace.IsFatal(ex))
                  throw InfoCardTrace.ThrowHelperError((Exception) new InvalidCardException(SR.GetString("ServiceInvalidEncryptedClaimValues")));
                throw;
              }
            }
          }
        }
      }
    }

    public InfoCardClaimCollection Clone()
    {
      InfoCardClaimCollection cardClaimCollection = new InfoCardClaimCollection(new Uri(this.m_infoCardId.OriginalString));
      cardClaimCollection.m_rowId = this.m_rowId;
      foreach (KeyValuePair<string, InfoCardClaim> keyValuePair in (Dictionary<string, InfoCardClaim>) this)
        cardClaimCollection[(string) keyValuePair.Key.Clone()] = keyValuePair.Value.Clone();
      return cardClaimCollection;
    }

    protected DataRow TryGetRow(StoreConnection con, QueryDetails details)
    {
      InfoCardTrace.Assert((Uri) null != this.m_infoCardId, "null infocard id");
      return con.GetSingleRow(details, new QueryParameter("ix_objecttype", new object[1]
      {
        (object) 2
      }), new QueryParameter("ix_parentid", new object[1]
      {
        (object) GlobalId.DeriveFrom(this.m_infoCardId.ToString())
      }));
    }
  }
}
